package cluster

import (
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

type ClusterInfo struct {
	ClusterId   string `json:"clusterId"`
	DisplayName string `json:"displayName"`
	City        string `json:"city"`
	District    string `json:"district"`
}

// 定义一个结构体，用于描述创建集群的配置信息

type ClusterConfing struct {
	ClusterInfo
	KubeConfig string `json:"kubeconfig"`
}

// 描述集群的状态
type ClusterStatus struct {
	ClusterInfo
	Version string `json:"version"`
	Status  string `json:"status"`
}

// 结构体的方法，用于判断集群的状态

func (c *ClusterConfing) getClusterStatus() (ClusterStatus, error) {
	// 判断集群是不是正常
	ClusterStatus := ClusterStatus{}
	ClusterStatus.ClusterInfo = c.ClusterInfo
	// 创建一个clientSet
	// 通过字符串形式接收kubeconfig
	restConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(c.KubeConfig))
	if err != nil {
		return ClusterStatus, err
	}
	clientset, err := kubernetes.NewForConfig(restConfig)
	if err != nil {
		return ClusterStatus, err
	}

	serverVersion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		return ClusterStatus, err
	}

	ClusterStatus.Version = serverVersion.String()
	ClusterStatus.Status = "Active"
	return ClusterStatus, nil
}
